package hottargui.config;

import hottargui.framework.Game;
import hottargui.framework.GameObjectServer;
import hottargui.framework.IllegalActionException;
import hottargui.framework.PlayerColor;
import hottargui.framework.Position;
import hottargui.framework.State;
import hottargui.framework.TarguiState;
import hottargui.framework.TurnSequenceStrategy;
import hottargui.standard.StandardPlayer;
import hottargui.standard.StandardTile;
import hottargui.framework.GameData;

public class AlphaNewGameState implements TarguiState {
	
	Game gameContext;
	
	public AlphaNewGameState(Game game)
	{
		this.gameContext = game;
	}
	
	@Override
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public State getState() {
		return State.newGame;
	}

	@Override
	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public void newGame() throws IllegalActionException {
		// Setup board
		GameData gameData = gameContext.getGameObjectServer().getGameData();
		gameContext.getGameObjectServer().getBoardStrategy().setupBoard(gameData);
		StandardPlayer sp;
		
		//Set up the players and which settlements they own
		sp = new StandardPlayer(PlayerColor.Red);
		sp.setSettlement(new Position(0,0));
		gameData.getPlayers().put(PlayerColor.Red, sp);
		
		sp = new StandardPlayer(PlayerColor.Green);
		sp.setSettlement(new Position(0,6));
		gameData.getPlayers().put(PlayerColor.Green, sp);
		
		sp = new StandardPlayer(PlayerColor.Blue);
		sp.setSettlement(new Position(6,0));
		gameData.getPlayers().put(PlayerColor.Blue, sp);
		
		sp = new StandardPlayer(PlayerColor.Yellow);
		sp.setSettlement(new Position(6,6));
		gameData.getPlayers().put(PlayerColor.Yellow, sp);

		//setup the settlements on the board
		//Everybody is given 10 camels when the game starts
		StandardTile settlement = (StandardTile)gameContext.getTile(new Position(0,0)); 
		settlement.changeUnitCount(10);
		settlement = (StandardTile)gameContext.getTile(new Position(0,6));
		settlement.changeUnitCount(10);
		settlement = (StandardTile)gameContext.getTile(new Position(6,0));
		settlement.changeUnitCount(10);
		settlement = (StandardTile)gameContext.getTile(new Position(6,6));
		settlement.changeUnitCount(10);		
		turnCard(); //Set the State to Move at changes the current Player
		
		//Round number one is started, Roundsleft should be 24
		//Maybe this should be done some other way
		gameData.setRoundsToPlay(gameData.getRoundsLeft() -1);
	}

	@Override
	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public PlayerColor turnCard() throws IllegalActionException {
		//set the new state
		gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
				
		//get the next player
		GameObjectServer gos = gameContext.getGameObjectServer();
		TurnSequenceStrategy turnSequence = gos.getTurnSequenceStrategy();
		gos.getGameData().setCurrentPlayer(turnSequence.getNextPlayerColor());		
		return gos.getGameData().getCurrentPlayer().getColor();
	}

	@Override
	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

}
